package samples.d3.image 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BitmapDataChannel;
	import flash.geom.Point;
	import samples.BaseSample;
	import utils.GeomUtils;
	
	/**
	 * ...
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class TimeSlices extends BaseSample 
	{
		
		
		[Embed(source='../../../../lib/photos/téléchargement.jpg')]private var src0:Class;
		[Embed(source='../../../../lib/photos/téléchargement (1).jpg')]private var src1:Class;
		[Embed(source='../../../../lib/photos/téléchargement (2).jpg')]private var src2:Class;
		[Embed(source = '../../../../lib/photos/téléchargement (3).jpg')]private var src3:Class;
		
		private var list:Array = [ new src0(), new src1(), new src2(), new src3() ];
		private var bd:BitmapData;
		private var bmp:Bitmap;
		private var redArray:Array;
		private var greenArray:Array;
		private var blueArray:Array;
		public function TimeSlices() 
		{
			bmp = new src1();
			addChild( bmp );
			
			bd = new BitmapData( bmp.width, bmp.height, true, 0 );
			addChild( new Bitmap( bd ) );
			
			redArray = new Array(256);
			greenArray = new Array(256);
			blueArray = new Array(256);
			center();
			//interval = setInterval( update, 30 );
			update();
			
		}
		
		private function update():void 
		{
			var val:int;
			var colNum:int = 16;// ( _stage.mouseY / sh ) * 0x10;
			for (var i:uint = 0; i < 0xFF; i++)
			{
				val = int( ( GeomUtils.map( (i+1), 0, 0xFF, 0, colNum ) ) );
				val *= (0xFF / colNum );
				
				redArray[i] 	= 0xFF000000 | val << 16;
				greenArray[i] 	= 0xFF000000 | val << 8;
				blueArray[i] 	= 0xFF000000 | val;
				
			}
			
			//bd.fillRect( bd.rect, 0 );
			//bd.draw( bmp );
			
			var tmp:BitmapData = bd.clone();
			var noise:BitmapData = bd.clone();
			var alphaMap:BitmapData = bd.clone();
			
			noise.perlinNoise( 200, 200, 3, 0, true, true, 1, true );
			noise.paletteMap( noise, noise.rect, new Point(), redArray, greenArray, blueArray);
			
			var k:int = 0;
			for ( i = 0; i < 0xFF; i += 16 )
			{
			
			
				tmp.fillRect( tmp.rect, 0 );
				tmp.draw( list[ k++ % list.length ] );
				
				alphaMap.fillRect( alphaMap.rect, 0 );
				alphaMap.draw( noise );
				
				tmp.threshold( noise, noise.rect, new Point(), '!=', 0xFF<<24 | i << 16 | i << 8 | i, Math.random() * 0xFFFFFFFF );
				
				//tmp.copyChannel( alphaMap, alphaMap.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.RED );
				//tmp.copyChannel( alphaMap, alphaMap.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.GREEN );
				//tmp.copyChannel( alphaMap, alphaMap.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.BLUE );
				//tmp.copyChannel( alphaMap, alphaMap.rect, new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA );
				
				bd.draw( tmp );
				
			//	bd.threshold( bd, bd.rect, new Point(), '<=', 0xFF000000 | val << 16 | val << 8 | val, ;
				
			}
			
			
		}
		
	}

}